package view;

import gui.TableContainer;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import model.GameRoundStatsInterface;
import model.PlayerGameStatsInterface;
import toolkit.Parse;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Statistics extends View {
	private JTextField fieldWinLoss;
	private JTextField fieldPlayedRounds;
	private JLabel labelPlayedRounds;
	private JTextField fieldAverageWinLoss;
	private TableContainer tableGameRounds;
	private JLabel labelAverageWinLoss;
	private JLabel labelWinLoss;
	
	protected controller.Statistics controller;
	
	private int gameId;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new Statistics());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public Statistics() {
		super();
		initGUI();
	}

	public Statistics(controller.Statistics controller, int gameId) {
		super();
		this.controller = controller;
		this.gameId = gameId;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setLayout(null);
			{
				fieldWinLoss = new JTextField();
				this.add(fieldWinLoss);
				fieldWinLoss.setBounds(198, 12, 190, 21);
				fieldWinLoss.setEnabled(false);
			}
			{
				labelWinLoss = new JLabel();
				this.add(labelWinLoss);
				labelWinLoss.setBounds(12, 15, 180, 14);
				labelWinLoss.setText("Total win/loss");
			}
			{
				fieldPlayedRounds = new JTextField();
				this.add(fieldPlayedRounds);
				fieldPlayedRounds.setBounds(198, 39, 190, 21);
				fieldPlayedRounds.setEnabled(false);
			}
			{
				labelPlayedRounds = new JLabel();
				this.add(labelPlayedRounds);
				labelPlayedRounds.setBounds(12, 42, 180, 14);
				labelPlayedRounds.setText("Played rounds");
			}
			{
				fieldAverageWinLoss = new JTextField();
				this.add(fieldAverageWinLoss);
				fieldAverageWinLoss.setBounds(198, 66, 190, 21);
				fieldAverageWinLoss.setEnabled(false);
			}
			{
				labelAverageWinLoss = new JLabel();
				this.add(labelAverageWinLoss);
				labelAverageWinLoss.setBounds(12, 69, 180, 14);
				labelAverageWinLoss.setText("Average win/loss");
			}
			{
				tableGameRounds = new TableContainer();
				this.add(tableGameRounds);
				tableGameRounds.setBounds(12, 95, 376, 193);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void postInitGUI()
	{
		PlayerGameStatsInterface statistics = controller.getPlayerGameStatistics();
		
		if (statistics != null)
		{
			fieldWinLoss.setText(Parse.toString(statistics.getTotalWinLoss()));
			fieldPlayedRounds.setText(Parse.toString(statistics.getPlayedRounds()));
			fieldAverageWinLoss.setText(Parse.toString(statistics.getAverageWinLoss()));
			
			ArrayList<GameRoundStatsInterface> gameRounds = statistics.getRoundStats();
			
			String[] columns = {"Bet", "Win/loss"};
			
			int gamesSize = gameRounds.size();
			
			Object[][] rows = new Object[gamesSize][3];
			
			for (int i = 0; i < gamesSize; i++)
			{
				GameRoundStatsInterface gameRound = gameRounds.get(i);
				
				Object[] row = {gameRound.getBet(), gameRound.getWinLoss()};
				
				rows[i] = row;
			}
			
			tableGameRounds.getModel().setData(columns, rows);
		}
	}
}
